﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using HRM.Common;
using HRM.BusinessLogic;

namespace HRM
{
    public partial class frmWorkingBachgroundInfo : Form
    {
        HRM.BusinessLogic.WorkingBackgroundInfo _WBackgroundInfo;
        int _EmployeeID;
        bool isChange = false;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        //WorkingBackgroundClient wcfWBackground;
        public frmWorkingBachgroundInfo(int WBackgroundID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            LoadLanguage();
            dlgResult = DialogResult.Cancel;



            _WBackgroundInfo = new HRM.BusinessLogic.WorkingBackgroundInfo();

            //Lấy theo kiểu gọi qua service cũ
            //wcfWBackground = Service.WorkingBackground();

            _WBackgroundInfo.WB_ID = WBackgroundID;
            _EmployeeID = EmployeeID;

            if (WBackgroundID > 0)
            {
                //cách cũ lấy qua service
                //_WBackgroundInfo = wcfWBackground.WorkingBackground_GetInfo(WBackgroundID);

                _WBackgroundInfo = WorkingBackground_Controller.Instance().WorkingBackground_GetInfo(WBackgroundID);

                Info2Form();
            }
            
            //chuyển mới
            
            
            
            listRole = _listRole;



        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        void Info2Form()
        {
            txtBackground_Name.Text = _WBackgroundInfo.WB_CompanyName;
            txtBackground_Position.Text = _WBackgroundInfo.WB_Position;
            txtBackground_StartDate.Text = _WBackgroundInfo.WB_StartDate.ToString("MM/yyyy");
            txtBackground_EndDate.Text = _WBackgroundInfo.WB_EndDate.ToString("MM/yyyy");
            txtBackground_Description.Text = _WBackgroundInfo.WB_Description;
            isChange = false;
        }

        void Form2Info()
        {
            _WBackgroundInfo.WB_EmployeeID = _EmployeeID;
            _WBackgroundInfo.WB_CompanyName = txtBackground_Name.Text.Trim();
            _WBackgroundInfo.WB_Position = txtBackground_Position.Text.Trim();
            _WBackgroundInfo.WB_StartDate = DateTime.ParseExact(txtBackground_StartDate.Text, "MM/yyyy", null);
            _WBackgroundInfo.WB_EndDate = DateTime.ParseExact(txtBackground_EndDate.Text, "MM/yyyy", null);
            _WBackgroundInfo.WB_Description = txtBackground_Description.Text.Trim();

        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            DateTime DateValue;

            if (txtBackground_Name.Text.Trim().Length == 0)
            {
                txtBackground_Name.Focus();
                MessageBox.Show(UpdateUI.GetString("tennoilamvieckhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (txtBackground_Position.Text.Trim().Length == 0)
            {
                txtBackground_Position.Focus();
                MessageBox.Show(UpdateUI.GetString("chuvukhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!DateTime.TryParseExact(txtBackground_StartDate.Text, "MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            {
                txtBackground_StartDate.Focus();
                MessageBox.Show("Thời gian bắt đầu không hợp lệ!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!DateTime.TryParseExact(txtBackground_EndDate.Text, "MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            {
                txtBackground_EndDate.Focus();
                MessageBox.Show("Thời gian kết thúc không hợp lệ!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            //if (DateTime.Compare(UISetting.GetDate(txtBackground_EndDate.Text), UISetting.GetDate(txtBackground_StartDate.Text)) < 0)
            //{
            //    MessageBox.Show(UpdateUI.GetString("ngayketthucphaimuonhonngaybatdau"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            //WorkingBackgroundClient wcfWBackground = Service.WorkingBackground();
            Form2Info();
            if (_WBackgroundInfo.WB_ID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (HRM.BusinessLogic.WorkingBackground_Controller.Instance().WorkingBackground_Update(_WBackgroundInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                //_WBackgroundInfo.ID = wcfWBackground.WorkingBackground_Insert(_WBackgroundInfo);
                _WBackgroundInfo.WB_ID = HRM.BusinessLogic.WorkingBackground_Controller.Instance().WorkingBackground_Insert(_WBackgroundInfo);


                if (_WBackgroundInfo.WB_ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_WBackgroundInfo.WB_ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }

            _WBackgroundInfo = new HRM.BusinessLogic.WorkingBackgroundInfo();
            _WBackgroundInfo.WB_ID = 0;
            txtBackground_Name.Text = "";
            txtBackground_Position.Text = "";
            txtBackground_StartDate.Text = "";
            txtBackground_EndDate.Text = "";
            txtBackground_Description.Text = "";


        }

        private void frmWorkingBachgroundInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            //wcfWBackground.Close();
            this.DialogResult = dlgResult;
        }

        private void frmWorkingBachgroundInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
